package serialization;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;

public class Demo1 {

	public static void main(String[] args) {

		FileOutputStream fos = null;
		ObjectOutputStream oos = null;
		FileInputStream fis = null;
		ObjectInputStream ois = null;
		try {
			User1 user = new User1();
			user.setId(1);
			user.setName("Tony");
			user.setPhone("13888888888");
			user.setBirthday(new Date());
			user.setAddress("西苑路111号");

			// 序列化
			fos = new FileOutputStream("object.out");
			oos = new ObjectOutputStream(fos);
			oos.writeObject(user);
			oos.flush();

			// 反序列化
			fis = new FileInputStream("object.out");
			ois = new ObjectInputStream(fis);
			User1 user2 = (User1) ois.readObject();
			System.out.println(user2.toString());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				oos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

}
